package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.service.SetmealService;
import com.itheima.utils.QcloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
//    @Autowired
//    private JedisPool jedisPool;


    @RequestMapping("/deleteById/{id}")
    public Result deleteById( @PathVariable Integer id){
        System.out.println(id);
        try {
//            Integer id  = Integer.parseInt(ids);
            setmealService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL1);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        System.out.println(map);
        try {
            setmealService.edit(map);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }




    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        // 上传过来的实际文件名
        String filenameUP = imgFile.getOriginalFilename();
        int i = filenameUP.lastIndexOf(".");
        String suffix = filenameUP.substring(i);
        // 真实使用的文件名
        String fileName = UUID.randomUUID().toString()+suffix;
        long size = imgFile.getSize();
        try {
            QcloudUtils.putObject(fileName, (FileInputStream) imgFile.getInputStream(),size);
//            添加图片名到redis
//            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
//    @RequestMapping("/upload")
//    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
//        // 上传过来的实际文件名
//        String filenameUP = imgFile.getOriginalFilename();
//        String[] split = filenameUP.split("\\.");
//        String suffix = "."+split[split.length-1];
//        String prefix = UUID.randomUUID().toString();
//        // 真实使用的文件名
//        String fileName = prefix+suffix;
//        // 执行文件上传操作
//        File file = new File("a");
//        try {
//            imgFile.transferTo(file);
//            QcloudUtils.putObject(fileName,file);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
//        }
//        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
//    }
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        System.out.println("hello");
        try {
            setmealService.add(map);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("hello");
        try {
            //  如果用了分组,再第二页会搜不到第一页中的数据
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


}
